<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="课题名称" prop="ktName">
        <el-input size="mini"
                  v-model="queryParams.ktName"
                  placeholder="请输入课题名称"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课题编号" prop="ktBh">
        <el-input size="mini"
                  v-model="queryParams.ktBh"
                  placeholder="请输入课题编号"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="processStatus">
        <el-select v-model="queryParams.processStatus" placeholder="请选择状态" clearable size="mini">
          <el-option
            v-for="dict in dict.type.process_business_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="委托单位" prop="wtDw">
        <el-input size="mini"
                  v-model="queryParams.wtDw"
                  placeholder="请输入委托单位"
                  clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上报年度" prop="sbNd">
        <el-date-picker clearable size="mini"
                        v-model="queryParams.sbNd"
                        type="year"
                        value-format="yyyy"
                        placeholder="请选择上报年度">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 顶部按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zxgl:zxtb:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              v-loading="loading" :data="zxtbList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" width="50"/>
      <el-table-column label="上报年度" align="center" prop="sbNd" width="80"/>
      <el-table-column show-overflow-tooltip label="阶段时间" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ parseTime(scope.row.sbSsQTime, '{y}-{m}-{d}') + '~' + parseTime(scope.row.sbSsZTime, '{y}-{m}-{d}') }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="课题名称" align="center" prop="ktName"/>
      <el-table-column show-overflow-tooltip label="课题编号" align="center" prop="ktBh"/>
      <el-table-column show-overflow-tooltip label="委托单位" align="center" prop="wtDw"/>
      <el-table-column show-overflow-tooltip label="项目负责人" align="center" prop="xmFzName"/>
      <el-table-column show-overflow-tooltip label="上报人员" align="center" prop="sbRy"/>
      <el-table-column label="状态" align="center" prop="processStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.process_business_status" :value="scope.row.processStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleJinDu(scope.row)"
            v-if="scope.row.processStatus !== 1"
          >查询进度</el-button>
          <el-link type="danger" disabled v-else>{{ '未提交审批' }}</el-link>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 进度组件 -->
    <Jindu ref="picture"/>

    <!-- 执行填报详情 -->
    <ZxTbXq ref="zxTbXq"/>
  </div>
</template>

<script>
import {
  listZxtb,
} from "@/api/xmgl/zxgl/zxtb";
import {getToken} from "@/utils/auth";
import ZxTbDr from "@/views/xmgl/zxgl/zxqktb/components/ZxTbDr";
import HtDx from "@/views/components/htXz/htDx";
import Jindu from "@/views/components/liuChengState/jindu";
import ZxTbXq from "@/views/xmgl/zxgl/zxqktb/components/ZxTbXq";

export default {
  name: "zxQkCx",
  components: {ZxTbXq, Jindu, HtDx, ZxTbDr},
  dicts: ['process_business_status'],
  data() {
    return {
      selectedRow: null, // 保存当前选中的行
      activeName: 'JbZl', //默认打开的标签
      xg: false, //判断是新增还是修改 (false新增 true修改)
      allFilesUploaded: false, // 添加这个来追踪所有文件是否上传完成
      fjUpload: false, //附件上传弹窗
      zxtbIdEdit: "", //要修改的 执行填报ID
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
      fileList: [],
      headers: {Authorization: "Bearer " + getToken()},
      number: 0,
      uploadList: [],
      fileType: ["doc", "xls", "ppt", "txt", "pdf", "docx", "zip"],
      getZxTbId: null, //后端获取的执行填报主键ID (UUID)
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedZxglZxtbFj: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 执行情况填报表格数据
      zxtbList: [],
      // 执行填报附件表格数据
      zxglZxtbFjList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        idWhere: null,
        ktName: null,
        ktBh: null,
        wtDw: null,
        xmFzName: null,
        sbRy: null,
        sbNd: null,
        userId: this.$store.state.user.id,
        deptId: null,
        processStatus: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleButtonClick(row) {
      this.$refs.zxTbXq.handleOpen(row);
    },
    //查询进度
    handleJinDu(row) {
      this.$refs.picture.handleOpen(row);
    },
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    // 获取文件名原名
    getFileNames(row) {
      // 从路径中提取文件名
      return row.fileName.split('/').pop(); // 返回文件名部分
    },
    // 获取文件名称
    getFileName(name) {
      // 如果是url那么取最后的名字 如果不是直接返回
      if (name.lastIndexOf("/") > -1) {
        return name.slice(name.lastIndexOf("/") + 1);
      } else {
        return name;
      }
    },
    /** 查询执行情况填报列表 */
    getList() {
      this.loading = true;
      listZxtb(this.queryParams).then(response => {
        this.zxtbList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      let ids = selection.map(item => item.zxtbId)
      if (ids.length === 1) {
        this.ids = ids[0]
      } else {
        this.ids = ids
      }
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zxgl/zxtb/export', {
        ...this.queryParams
      }, `zxtb_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
